package com.snow.order.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.snow.order.entity.Buy;
import com.snow.order.entity.Order;

public class OrderDao extends BasicDao {

	{
		TABLE_NAME = "T_ORDER";
	}

	private static class InnerSingleton {
//		public static OrderDao dao = new OrderDao();
	}

	public OrderDao() {

	}

//	public static OrderDao getInstence() {
//		return InnerSingleton.dao;
//	}

	/**
	 * tong ji wan cheng de ding dan
	 * 
	 * @return
	 */
	public int getCompletedOrderAmount() {
		Map<String, String> where = new HashMap<String, String>();
		List<Order> completedOrderList = query(null, where, new IQuery() {

			public List<Order> getListFromResultSet(ResultSet resultSet)
					throws SQLException {

				List<Order> result = new ArrayList<Order>();
				Order order;
				while (resultSet.next()) {
					order = new Order();

					result.add(order);
				}
				return result;
			}

		});

		return completedOrderList.size();
	}
	
	/**
	 * @author zhangqq
	 * @param iPageNo
	 * @param no
	 * @return
	 * @throws Exception
	 */
	public List<Order> getAllFood(int no) throws Exception {		//find by orderno
		// TODO Auto-generated method stub

		List<Order> byList = null;
		String sql = "select * from T_ORDER where no=?";
		this.openconnection();
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, no);
		ResultSet rs = ps.executeQuery();
		if (rs != null) {
			byList = new ArrayList<Order>();
			while (rs.next()) {

				Order by = new Order();
				by.setNo(rs.getInt("NO"));
				by.setSum(rs.getDouble("SUM"));
				by.setTime(rs.getString("TIME"));
				by.setCommt(rs.getString("commt"));
				by.setStatus(rs.getString("status"));
				byList.add(by);
			}
		}
		System.out.println(byList);
		System.out.println(no);
		return byList;
	}

	
	public void Commit(String commt,int no) throws Exception{
		String sql = "update t_order set status = 'd',commt=? where no=?";
		this.openconnection();
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, commt);
		ps.setInt(2, no);
		int i = ps.executeUpdate();
		System.out.println(i);
	}
}
